package com.hurl.stock.util;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.hurl.stock.persistence.PersistentBO;

public class HibernateExecutor {
	Session session = null;
	
	public HibernateExecutor(SessionFactory sessionFactory){
		session = sessionFactory.getCurrentSession();
	}
	
	public void beginTransaction(){
		session.beginTransaction();
	}
	
	public void evict(PersistentBO bo){
		session.evict(bo);
	}
	
	public void flush(){
		session.flush();
		session.clear();
	}
	public void commit(){
		session.getTransaction().commit();
	}
	
	public void save(PersistentBO bean){
		session.save(bean);
	}
	
	public <T extends PersistentBO> List<T> findAllBOList(Class<T> persistentClass){
		return session.createCriteria(persistentClass).list();
	}
	public Criteria createCriteria(Class<? extends PersistentBO> persistentClass){
		return session.createCriteria(persistentClass);
	}
	
	public void updateHQL(String queryString,Object[] params){
		Query query = session.createQuery(queryString);
		if(params!=null && params.length>0){
			for(int i=0;i<params.length;i++){
				query.setParameter(i, params[i]);
			}
		}
		query.executeUpdate();
	}
	
	public void updateSQL(String queryString,Object[] params){
		SQLQuery query = session.createSQLQuery(queryString);
		if(params!=null && params.length>0){
			for(int i=0;i<params.length;i++){
				query.setParameter(i, params[i]);
			}
		}
		query.executeUpdate();
	}
}
